package com.mowh.gof23.behavioral.patterns.visitor;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author mowh1gd
 * @create 2023/10/20 10:05
 */
public class Directory extends Entry{
    private String name;
    private ArrayList dir = new ArrayList();

    public Directory(String name) {
        this.name = name;
    }

    @Override
    public void accept(Visitor visitor) { //子类Directory实现Element接口，传入的visitor调用visit方法完成功能。
        visitor.visit(this);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getSize() {
        int size = 0;
        Iterator it = dir.iterator();
        while (it.hasNext()) {
            Entry entry = (Entry) it.next();
            size += entry.getSize();
        }
        return size;
    }

    public Entry add(Entry entry) {
        dir.add(entry);
        return this;
    }

    public Iterator iterator() {
        return dir.iterator();
    }
}
